
package com.egame.manage.biz.web.controller.gc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.model.PageData;
import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.gc.GameTagInfo;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/tv/tag")
public class NewGameTagController extends BaseController {
	public NewGameTagController(){		
	}
	public NewGameTagController(HttpServletRequest req, HttpServletResponse resp) {
		super(req, resp);
	}

	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView listAllTag(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		GameTagType type = GameTagType.lookup(WebUtils.getInt(req, "tagType", 1));
		int page = WebUtils.getInt(req, "page", 0);
		int rowsOfPage = WebUtils.getInt(req, "rowsOfPage", 15);
		PageData pd = ManageClient.getInstance().pageGameTagInfoByTagType(appId, loginUserId, type, page, rowsOfPage);
		ModelAndView mv = new ModelAndView("/new/op/tagList");
		mv.addObject("tagType", type.value());
		mv.addObject("pdUrl", "/tv/tag/list");
		mv.addObject("pd", pd);
		return mv;		
	}	

	@RequestMapping(value = "/add/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showAddPage(HttpServletRequest req, HttpServletResponse resp) throws Exception{		
		GameTagType type = GameTagType.lookup(WebUtils.getInt(req, "tagType", 0));
		ModelAndView mv = new ModelAndView("/new/op/newTag");
		mv.addObject("tagType", type.value());
		return mv;
	}
	
	@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView add(GameTagInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		GameTagType type = GameTagType.lookup(WebUtils.getInt(req, "tagTypeNum", 0));
		bean.setTagType(type);
		GameTagServiceImpl.getInstance().addTag(appId, loginUserId, bean);	
		return listAllTag(req,resp);
	}

	@RequestMapping(value = "/delete", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteTag(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int tagId = WebUtils.getInt(req,"tagId",0);		
		GameTagServiceImpl.getInstance().deleteTag(appId, loginUserId, tagId);	
		return listAllTag(req,resp);
	}
	
	@RequestMapping(value = "/link/add", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView addTagLink(GameTagInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int position = WebUtils.getInt(req,"tagid",0);
		int tagId = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, position);	
		String gIds = WebUtils.getString(req, "gid","");
		if(!Utils.stringIsNullOrEmpty(gIds)){
			String[] ids= gIds.split(",");
			for(String id:ids){
				GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, Integer.valueOf(id), tagId ,0 ,-1);
			}
		}
		return listAllTag(req,resp);
	}

	@RequestMapping(value = "/link/delete", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView deleteTagLink(GameTagInfo bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int position = WebUtils.getInt(req,"tagid",0);
		int tagId = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, position);	
		String gIds = WebUtils.getString(req, "gid","");
		if(!Utils.stringIsNullOrEmpty(gIds)){
			String[] ids= gIds.split(",");
			for(String id:ids){
				GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, Integer.valueOf(id), tagId);
				int flagId = ManageClient.getInstance().getGameTagIdByGameTagName(appId, loginUserId, GameTagType.in_tag, cn.egame.interfaces.ConstVar.HOT_LABEL);				
				GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, Integer.valueOf(id), flagId);
				flagId = ManageClient.getInstance().getGameTagIdByGameTagName(appId, loginUserId, GameTagType.in_tag, cn.egame.interfaces.ConstVar.NEW_LABEL);				
				GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, Integer.valueOf(id), flagId);
			}
		}		
		return listAllTag(req,resp);
	}
	
	@ResponseBody
	@RequestMapping(value = "/hotflag/set", method = {RequestMethod.GET,RequestMethod.POST})
	public int setHotFlag(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int tagId = ManageClient.getInstance().getGameTagIdByGameTagName(appId, loginUserId, GameTagType.in_tag, cn.egame.interfaces.ConstVar.HOT_LABEL);
		String gIdString = WebUtils.getString(req, "gid", "");
		String hotflagString = WebUtils.getString(req, "hotflag", "");		
		if(!Utils.stringIsNullOrEmpty(gIdString) && !Utils.stringIsNullOrEmpty(hotflagString)){
			String[] gids = gIdString.split(",");
			String[] hotflags = hotflagString.split(",");
			if(gids.length==hotflags.length){
				for(int i=0;i<gids.length;i++){
					if(Integer.valueOf(hotflags[i])==1){
						GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, Integer.valueOf(gids[i]), tagId ,0,-1);
					}else{
						GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, Integer.valueOf(gids[i]), tagId );
					}
				}
				return 1;
			}
		}	
		return -1;
	}
	
	@ResponseBody
	@RequestMapping(value = "/newflag/set", method = {RequestMethod.GET,RequestMethod.POST})
	public int setNewFlag(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int tagId = ManageClient.getInstance().getGameTagIdByGameTagName(appId, loginUserId, GameTagType.in_tag, cn.egame.interfaces.ConstVar.NEW_LABEL);
		String gIdString = WebUtils.getString(req, "gid", "");
		String newflagString = WebUtils.getString(req, "newflag", "");		
		if(!Utils.stringIsNullOrEmpty(gIdString) && !Utils.stringIsNullOrEmpty(newflagString)){
			String[] gids = gIdString.split(",");
			String[] newflags = newflagString.split(",");
			if(gids.length==newflags.length){
				for(int i=0;i<gids.length;i++){
					if(Integer.valueOf(newflags[i])==1){
						GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, Integer.valueOf(gids[i]), tagId ,0, -1);
					}else{
						GameTagServiceImpl.getInstance().deleteTagLink(appId, loginUserId, Integer.valueOf(gids[i]), tagId);
					}
				}
				return 1;
			}
		}	
		return -1;
	}
	
	@ResponseBody
	@RequestMapping(value = "/upper", method = {RequestMethod.GET,RequestMethod.POST})
	public int upperGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gId = WebUtils.getInt(req,"gid",0);
		int tagid = WebUtils.getInt(req, "tagid", 0);
		tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, tagid);
		return GameTagServiceImpl.getInstance().upper(appId, loginUserId, gId, tagid);
	}
	
	@ResponseBody
	@RequestMapping(value = "/lower", method = {RequestMethod.GET,RequestMethod.POST})
	public int lowerGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gId = WebUtils.getInt(req,"gid",0);
		int tagid = WebUtils.getInt(req, "tagid", 0);
		tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, tagid);
		return GameTagServiceImpl.getInstance().lower(appId, loginUserId, gId, tagid);
	}

	@ResponseBody
	@RequestMapping(value = "/top", method = {RequestMethod.GET,RequestMethod.POST})
	public int topGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gId = WebUtils.getInt(req,"gid",0);
		int tagid = WebUtils.getInt(req, "tagid", 0);
		tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, tagid);
		return GameTagServiceImpl.getInstance().top(appId, loginUserId, gId, tagid);
	}

	@ResponseBody
	@RequestMapping(value = "/down", method = {RequestMethod.GET,RequestMethod.POST})
	public int downGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{		
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gId = WebUtils.getInt(req,"gid",0);
		int tagid = WebUtils.getInt(req, "tagid", 0);
		tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, tagid);
		return GameTagServiceImpl.getInstance().down(appId, loginUserId, gId, tagid);
	}
	
	@ResponseBody
	@RequestMapping(value = "/search", method = {RequestMethod.GET,RequestMethod.POST})
	public List<String> searchName(HttpServletRequest req, HttpServletResponse resp) throws Exception{		
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		return GameTagServiceImpl.getInstance().getNameList();
	}
}
